/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.controller;

import com.lehe.common.enums.GoldCoinEnum;
import com.lehe.common.exceptions.LHException;
import com.lehe.common.utils.StringUtil;
import com.lehe.dao.XHUserListDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lehe.common.MessageEnumDto;
import com.lehe.entry.AgentInfoDto;
import com.lehe.entry.UserInfoDto;
import com.lehe.entry.xh.UserDto;
import com.lehe.service.impl.AgentInfoService;
import com.lehe.service.impl.UserInfoService;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * 代理管理
 *
 * @author 王丹
 * @since 2017-02-26
 */
@Controller
@RequestMapping("agent")
public class AgentManagerController extends Basecontroller{

    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private XHUserListDao xHUserListDao;

    
    /**
     * 授权代理首页
     * @return
     */
    @RequestMapping
    public String index(){
        return "user/auth";
    }

    /**
     * 授权玩家信息查询
     * @param uid
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public UserDto query(@RequestParam(value="uid") String uidss){
    	
    	Long uid;
		try {
			uid = Long.parseLong(uidss);
		} catch (NumberFormatException e) {
			UserDto d = new UserDto();
            d.setMsg("用户ID"+uidss+"不存在");
            return d;
		}
    	List<UserDto> result = xHUserListDao.queryUserList(null, null, uid, null, null, null, null, null, null, null);
        if(result == null || result.isEmpty()){
        	UserDto d = new UserDto();
            d.setMsg("用户ID"+uidss+"不存在");
            return d;
        }
        
        UserDto d = result.get(0);
        if(d.getParentId() == null || !d.getParentId().equals(this.getUserId())){
        	d = new UserDto();
            d.setMsg("用户ID"+uid+"不属于当前用户的下级");
            return d;
        }
        
        if(d.getUtype() == 1){
        	d = new UserDto();
            d.setMsg("用户ID"+uid+"已经是代理");
            return d;
        }
        return d;
    }

    /**
     * 确定授权操作
     * @param uid
     * @param pwd
     * @param mobile
     * @return
     */
    @RequestMapping("auth")
    @ResponseBody
    public MessageEnumDto authAgent(@RequestParam(value="uid") Long uid){
       
        return agentInfoService.createAgent(this.getUserId(), uid);
    }

  
    
    /**
     * 转移金币确认
     * @param uid
     * @param count
     * @return
     */
    @RequestMapping("charge/confrim")
    @ResponseBody
    public MessageEnumDto chargeConfrim(@RequestParam(value="uid") Long uid,Integer count){
      
        return agentInfoService.charge(this.getUserId(), uid,count);
    }

    /**
     * 代理购币
     * @param m
     * @return
     */
    @RequestMapping("buyGoldCoin")
    public String buyGoldCoin(Model m){
        UserInfoDto dto = userInfoService.queryUserInfo(this.getUserId(), null);
        m.addAttribute("userInfo", dto);
        return "user/buyGoldCoin";
    }

    /**
     * 代理购币确认支付
     * @param wxOpenId
     * @param goldLv
     * @param request
     * @return
     * @throws LHException
     */
    @RequestMapping("confirmPay")
    @ResponseBody
    public Map<String, String> confirmPay(String wxOpenId, String goldLv, HttpServletRequest request) throws LHException {
        return agentInfoService.confirmPay(this.getUserId(), wxOpenId, goldLv, request.getRemoteAddr());
    }

}
